﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.ResourceManagement.AsyncOperations;
//12.三种异步操作处理
//Completed事件
//协程
//await/async
public class Example5 : MonoBehaviour
{

    public AssetReference A;
    public AssetReference B;
    public AssetReference C;

    private void Start()
    {
        
    }


    private void OnGUI()
    {
        if (GUILayout.Button("Complete"))
        {
            Instance1();
        }

        if (GUILayout.Button("协程方式实现异步操作的同步"))
        {
            StartCoroutine(Instance2());
        }
        if (GUILayout.Button("async/await"))
        {
            Instance3();
        }
    }
    void Instance1()
    {
        A.InstantiateAsync().Completed+= args=> {
            B.InstantiateAsync().Completed += args2 => {
                C.InstantiateAsync().Completed += args3 => {

                };
            };
        };
    }

    IEnumerator Instance2()
    {
        var handle = A.InstantiateAsync();
        yield return handle;

        handle = B.InstantiateAsync();
        yield return handle;

        handle = C.InstantiateAsync();
        yield return handle;

    }
    async void Instance3()
    {
        var handle = A.InstantiateAsync();
        await handle.Task;

        handle = B.InstantiateAsync();
        await handle.Task;

        handle = C.InstantiateAsync();
        await handle.Task;
    }


}
